<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px"
				 label-position="left">
			<el-form-item v-if="mode==='show'" label="ID" prop="id">
				<el-input v-model="form.id" placeholder="" clearable></el-input>
			</el-form-item>
			<el-form-item label="接口名称" prop="name">
				<el-input type="textarea" v-model="form.name" placeholder="请输入接口名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="地址" prop="url">
				<el-input type="textarea" v-model="form.url" placeholder="请输入地址" clearable></el-input>
			</el-form-item>
			<el-form-item v-if="mode==='show'" label="创建时间" prop="createDatetime">
				<el-input v-model="form.createDatetime" placeholder=" " clearable></el-input>
			</el-form-item>
			<el-form-item v-if="mode==='show'" label="更新时间" prop="updateDatetime">
				<el-input v-model="form.updateDatetime" placeholder=" " clearable></el-input>
			</el-form-item>
			<el-form-item v-if="mode==='show'" label="接口文档" prop="updateDatetime">
				<el-input v-model="form.downloadUrl" placeholder=" " clearable></el-input>
			</el-form-item>
			<el-form-item v-if="mode!=='show'" label="上传文件" prop="updateDatetime">
				<sc-upload-file v-model="fileurl" :limit="1" :data="{otherData:'demo'}"
								tip="文件不要超过10M,请上传doc/docx/xls/xlsx格式文件" @success="handleSuccess">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button v-if="mode==='show'" :disabled="!this.form.downloadUrl" type="primary" @click="down()">下载接口文档
			</el-button>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {getDeptList} from "@/api/model/dept";
	import {addOrUpdate} from "@/api/model/jk";

	export default {

		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增接口',
					edit: '编辑接口',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: "",
					name: "",
					updateDatetime: "",
					url: "",
					createDatetime: "",
					downloadUrl: ""
				},
				//验证规则
				rules: {
					avatar: [
						{required: true, message: '请上传头像'}
					],
					userName: [
						{required: true, message: '请输入登录账号'}
					],
					name: [
						{required: true, message: '请输入接口名称'}
					],
					url: [
						{required: true, message: '请输入地址'}
					],
					password: [
						{required: true, message: '请输入登录密码'},
						{
							validator: (rule, value, callback) => {
								if (this.form.password2 !== '') {
									this.$refs.dialogForm.validateField('password2');
								}
								callback();
							}
						}
					],
					password2: [
						{required: true, message: '请再次输入密码'},
						{
							validator: (rule, value, callback) => {
								if (value !== this.form.password) {
									callback(new Error('两次输入密码不一致!'));
								} else {
									callback();
								}
							}
						}
					],
					dept: [
						{required: true, message: '请选择所属部门'}
					],
					group: [
						{required: true, message: '请选择所属角色', trigger: 'change'}
					]
				},
				//所需数据选项
				groups: [],
				sexs: [{
					'id': '10',
					'label': '男'
				}, {
					'id': '11',
					'label': '女'
				}],
				groupsProps: {
					value: "id",
					multiple: true,
					checkStrictly: true
				},
				depts: [],
				deptsProps: {
					value: "id",
					checkStrictly: true
				}
			}
		},
		mounted() {
			this.getGroup()
			this.getDept()
		},
		methods: {
			down() {
				if (this.form.downloadUrl) {
					window.open(this.form.downloadUrl);
				}
			},
			handleSuccess(res) {
				this.form.downloadUrl = res.data.downloadUrl
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//加载树数据
			async getGroup() {
				let data = [
					{
						"id": "1",
						"label": "管理员",
						"alias": "SA",
						"sort": 1,
						"status": "1",
						"remark": "管理员",
						"date": "2020-05-07 09:30:00"
					},
					{
						"id": "2",
						"label": "普通用户",
						"alias": "Business Administrator",
						"sort": 2,
						"status": "1",
						"remark": "普通用户",
						"date": "2020-05-07 09:30:00"
					},
				];
				this.groups = data;
			},
			async getDept() {
				getDeptList({
					"pageNum": 1,
					"pageSize": 99
				}).then(res => {
					this.depts = res.data.list;
				})
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						let requestData = {
							"name": this.form.name,
							"url": this.form.url,
							"docUrl": this.form.downloadUrl
						}
						if (this.mode !== 'add') {
							requestData.id = this.form.id
						}
						this.isSaveing = true;
						addOrUpdate(requestData).then((res) => {
							this.isSaveing = false;
							console.log(res)
							if (res.code === 200) {
								this.$emit('success', this.form, this.mode)
								this.visible = false;
								this.$message.success("操作成功")
							} else {
								this.$alert(res.msg, "提示", {type: 'error'})
							}
						}).catch((e) => {
							console.log(e)
							this.$alert('操作异常', "提示", {type: 'error'})
						})
					} else {
						return false;
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.form.id = data.id
				this.form.name = data.name
				this.form.url = data.url
				this.form.createDatetime = data.createDatetime
				this.form.updateDatetime = data.updateDatetime
				this.form.downloadUrl = data.docUrl

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
